<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Admin;

class Login extends Controller{
    //显示后台登录表
    public function login(){
        return $this->fetch();
    }

    //检验是否登录成功
    /**
     * 如果登录成功 设置 ：
     * username为登录标志
     */
    public function doLogin(){
        $username = input('post.username');
        $password = input('post.password');
        if(empty($username)){
            $this->error('用户名不能为空');
        }
        if(empty($password)){
            $this->error('密码不能为空');
        }
        $user=Admin::get($username);
        if(empty($user)){
            $this->error('您输入的用户名不存在!');
        }elseif($user->password == md5($password)){
            session('username',$username);
            $this->redirect(url('index/index'));
        }else{
            $this->error('您输入的密码错误!');
        }
    }
        
    //退出
    public function loginOut(){
        session('username',null);
        return $this->fetch('login');
    }

    //×××××××××××××
    public function checkLogin(){
        $username = session('username');
        if (empty($username)) {
            $this->error('请先登录!', 'login/login');
        }
        else{
            $backUser=$username;
            $data = Db::table('admin')->where('username',$backUser)->select();
            $user=Admin::get($username);
            switch ($user->authority)
            {
                case 1 :
                    $backAuth = "超级管理员";break;
                case 2 :
                    $backAuth = "管理员";break;
                case 3 :
                    $backAuth = "游客";break;
                    
            }
            $this->success('登录成功!','index/index');
        }
    }
}